package ddbs;

public class ProxyWork extends AbsWork implements JobDoneCallback {
    Peer from;
    LocalWork realwk;
    Sched sch;

    private LocalWork buildWorkFromMsg(MSG msg) {
        return WorkFactory.creatWork(msg);
    }

    ProxyWork(Sched s, Peer from, MSG msg) {
        this.id = msg.id;
        this.from = from;
        this.sch = s;
        this.realwk = buildWorkFromMsg(msg);
        from.addProxyWork(this);
        sch.addWork(realwk);
        this.realwk.assignTo(sch.getMgr().getSelf());
    }

    protected void dealWith() {
        realwk.donecb = this;
        realwk.dealWith();
    }

    @Override
    public void jobdonecb(Object obj) {
        from.sendWorkResult(this);
        from.rmProxyWork(this);
        sch.rmWork(realwk);
        done();
    }

    public MSG toMSG() {
        String content = String.valueOf(this.realwk.data.serialize());
        return new MSG(Sched.msgType, this.realwk.getStatus(), -id,
                content.length(), content);
    }
}
